package com.example.tms.controller;

import java.util.List;

import com.example.tms.common.annotation.LogAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.tms.common.enums.Result;
import com.example.tms.entity.Performance;
import com.example.tms.service.PerformanceService;
/**
 * 绩效考核控制器
 * 提供绩效考核的CRUD操作
 */
@RestController
@RequestMapping("/performances")
public class PerformanceController {

    @Autowired
    private PerformanceService performanceService;

    /**
     * 获取所有绩效考核
     * @return 包含所有绩效考核的结果
     */
    @LogAction("获取所有绩效考核信息")
    @GetMapping("")
    public Result selectAll() {
        List<Performance>  list =  performanceService.selectAll();
        return Result.success(list);
    }

    /**
     * 根据ID获取绩效考核详情
     * @param id 绩效考核ID
     * @return 包含考核详情的结果
     */
    @LogAction("获取绩效考核详情")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer perId) {
        Performance performance = performanceService.selectById(perId);
        return Result.success(performance);
    }

    /**
     * 创建新的绩效考核
     * @param performance 包含考核信息的对象
     * @return 操作结果
     */
    @LogAction("创建新的绩效考核")
    @PostMapping("")
    public Result add(@RequestBody Performance performance) {
        int result = performanceService.insert(performance);
        return result > 0 ? Result.success() : Result.error("添加失败");
    }

    /**
     * 更新绩效考核信息
     * @param id 考核ID
     * @param performance 包含更新信息的对象
     * @return 操作结果
     */
    @LogAction("更新绩效考核信息")
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer perId, @RequestBody Performance performance) {
        performance.setPerId(perId); // 将路径参数的 id 设置到 performance 对象中
        int result = performanceService.update(performance);
        return result > 0 ? Result.success() : Result.error("更新失败");
    }

    /**
     * 软删除绩效考核
     * @param id 考核ID
     * @return 操作结果
     */
    @LogAction(value = "软删除绩效考核", level = "WARN")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer perId) {
        int result = performanceService.deleteById(perId);
        return result > 0 ? Result.success() : Result.error("删除失败");
    }
}